using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GeneralInfoPanel : MonoBehaviour {
	
	public UILabel Name;
	public UILabel race;
	public UILabel classes;
	public UILabel experience;
	public UILabel nextLevelExp;
	public UISlider expFillBar;
	public UILabel alignment;
	public UILabel hp;
	public UILabel hpStatus;
	public UILabel speed;
	public UILabel DR;
	public UILabel SR;
	public UILabel str;
	public UILabel dex;
	public UILabel con;
	public UILabel Int;
	public UILabel wis;
	public UILabel cha;
	public UILabel fort;
	public UILabel refl;
	public UILabel will;
	public UILabel flySpeed;
	public UILabel flyManeuverability;
	public UILabel swimSpeed;
	public UILabel climbSpeed;
	public UILabel burrowSpeed;
	public UILabel hair;
	public UILabel eyes;
	public UILabel skin;
	public UILabel height;
	public UILabel size;
	public UILabel age;
	
	public UITable table;
	
	GameObject levelUpWindow;

	// Use this for initialization
	void Start () {
		ReLoadInfo();
		
		UIButtonMessage messenger = Name.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "NameClicked";
		
		messenger = race.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "RaceClicked";
		
		messenger = classes.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "ClassesClicked";
		
		messenger = classes.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "ClassesPressed";
		
		messenger = alignment.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "AlignmentClicked";
		
		messenger = hp.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "HealthClicked";
		
		messenger = speed.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "SpeedClicked";
		
		messenger = DR.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "DRClicked";
		
		messenger = SR.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "SRClicked";
		
		messenger = str.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "strPressed";
		
		messenger = str.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "strClicked";
		
		messenger = dex.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "dexPressed";
		
		messenger = dex.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "dexClicked";
		
		messenger = con.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "conPressed";
		
		messenger = con.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "conClicked";
		
		messenger = Int.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "IntPressed";
		
		messenger = Int.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "IntClicked";
		
		messenger = wis.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "wisPressed";
		
		messenger = wis.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "wisClicked";
		
		messenger = cha.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "chaPressed";
		
		messenger = cha.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "chaClicked";
		
		messenger = flySpeed.transform.parent.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "altMovementsClicked";
		
		messenger = hair.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "hairClicked";
		
		messenger = eyes.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "eyesClicked";
		
		messenger = skin.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "skinClicked";
		
		messenger = height.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "heightClicked";
		
		messenger = age.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "ageClicked";
		
		messenger = fort.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "fortClicked";
		
		messenger = refl.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "reflClicked";
		
		messenger = will.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.functionName = "willClicked";
		
		messenger = fort.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "fortPressed";
		
		messenger = refl.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "reflPressed";
		
		messenger = will.transform.parent.gameObject.AddComponent<UIButtonMessage>();
		messenger.target = this.gameObject;
		messenger.trigger = UIButtonMessage.Trigger.OnPressAndHold;
		messenger.functionName = "willPressed";
	}
	
	public void ReLoadInfo()
	{
		if(SheetManager.activeCharacter != null)
		{
			CharacterSheet a = SheetManager.activeCharacter;
			Name.text = a.name;
			NGUITools.AddWidgetCollider(Name.transform.parent.gameObject);
			
			if(a.race != null)
				race.text = a.race.name;
			else
				race.text = "Human";
			
			classes.text = a.classString;
			NGUITools.AddWidgetCollider(classes.transform.parent.gameObject);
			
			experience.text = a.Experience + "exp";
			nextLevelExp.text = a.nextLevelExperience + "exp";
			expFillBar.sliderValue = ((float)a.Experience) / ((float)a.nextLevelExperience);
			alignment.text = a.Alignment;
			hp.text = (a.HP-a.Damage) + "/" + (a.HP);
			hpStatus.text = a.hpStatus;
			speed.text = a.Speed.ToString();
			DR.text = a.extraInfo.DR + "/" + a.extraInfo.DRMaterial;
			SR.text = a.extraInfo.SpellResistance.ToString();
			str.text = a.Strength + "(" + a.Strength.Modifier() + ")";
			dex.text = a.Dexterity + "(" + a.Dexterity.Modifier() + ")";
			con.text = a.Constitution + "(" + a.Constitution.Modifier() + ")";
			Int.text = a.Intelligence + "(" + a.Intelligence.Modifier() + ")";
			wis.text = a.Wisdom + "(" + a.Wisdom.Modifier() + ")";
			cha.text = a.Charisma + "(" + a.Charisma.Modifier() + ")";
			flySpeed.text = a.extraInfo.FlySpeed.ToString();
			flyManeuverability.text = a.extraInfo.FlyManeuverability;
			swimSpeed.text = a.extraInfo.SwimSpeed.ToString();
			climbSpeed.text = a.extraInfo.ClimbSpeed.ToString();
			burrowSpeed.text = a.extraInfo.BurrowSpeed.ToString();
			hair.text = a.extraInfo.HairColor;
			eyes.text = a.extraInfo.EyeColor;
			skin.text = a.extraInfo.SkinColor;
			height.text = Mathf.FloorToInt(a.extraInfo.Height/12) + "'" + a.extraInfo.Height%12 + "\""; 
			size.text = a.Size;
			age.text = a.extraInfo.Age.ToString();
			
			fort.text = a.Fort.ToString();
			refl.text = a.Ref.ToString();
			will.text = a.Will.ToString();
			
			table.repositionNow = true;
		}
	}
	
	IEnumerator NameClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Name", Name.text, true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Name?",updates));
		
		if(!string.IsNullOrEmpty(updates[0].outVariable))
		{
			try
			{
				SheetManager.activeCharacter.name = updates[0].outVariable;
				SheetManager.SaveActiveCharacter(false);
				Name.text = updates[0].outVariable;
				table.repositionNow = true;
				NGUITools.AddWidgetCollider(Name.transform.parent.gameObject);
			}catch { }
		}
		
		yield break;
	}
	
	IEnumerator RaceClicked()
	{		
		List<string> races = new List<string>();
		foreach(var rl in RaceManager.index)
		{
			races.Add(rl.Key);
		}
		
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("", "", false, races)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Character Race?", updates));
		
		
		bool accepted = !string.IsNullOrEmpty(updates[0].outVariable);
		if(accepted)
		{
			try
			{
				Race r = RaceManager.Load(updates[0].outVariable);
				if(r != null)
				{
					SheetManager.activeCharacter.race = r;
					SheetManager.SaveActiveCharacter();
					ReLoadInfo();
				}
			}
			catch { };
		}
	}
	
	IEnumerator ClassesClicked()
	{
		List<string> options = new List<string>
		{
			"Add Experience",
			"Set Experience"
		};
		
		if(SheetManager.activeCharacter.nextLevelExperience <= SheetManager.activeCharacter.Experience)
		{
			options.Add("LEVEL UP!");
		}
		
		List<SetterWindowDescription> choice = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "", false, options, false)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Do what?", choice));
		
		if(!string.IsNullOrEmpty(choice[0].outVariable))
		{
			switch(choice[0].outVariable)
			{
			case "Add Experience":
				StartCoroutine(AddExp());
				break;
			case "Set Experience":
				StartCoroutine(SetExp());
				break;
			case "LEVEL UP!":
				LevelUp();
				break;
			}
		}
		
		yield break;
	}
	
	IEnumerator ClassesPressed()
	{
		List<SetterWindowDescription> classLevels = new List<SetterWindowDescription>();
		
		foreach(var i in ClassManager.index.Keys)
		{
			int currentLevel = 0;
			CharacterClass thisClass = SheetManager.activeCharacter.classes.Find(c => c.name == i);
			if(thisClass != null)
				currentLevel = thisClass.level;
			
			classLevels.Add(new SetterWindowDescription(i, currentLevel.ToString(), false));
		}
		
		if(classLevels.Count > 0)
		{
			yield return StartCoroutine(HelperFunctions.SetterWindow("Set Character Levels: ", classLevels));
			
			if(classLevels[0].outVariable != null)
			{
				foreach(SetterWindowDescription swd in classLevels)
				{
					CharacterClass thisClass = SheetManager.activeCharacter.classes.Find(c => c.name == swd.label);
					
					if(thisClass == null)
					{
						if(swd.outVariable == "0")
							continue;
						
						thisClass = ClassManager.Load(swd.label);
						thisClass.level = 0;
						SheetManager.activeCharacter.classes.Add(thisClass);
					}
					
					if(thisClass.level == int.Parse(swd.outVariable))
						continue;
					
					string levelingInfo = thisClass.SetLevel(Mathf.Min(Mathf.Max(int.Parse(swd.outVariable), 0), 20));
					
					yield return StartCoroutine(HelperFunctions.ConfirmationWindow(levelingInfo, null));
					
					if(thisClass.level <= 0)
					{
						SheetManager.activeCharacter.classes.Remove(thisClass);
					}
				}
			}
		}
		
		ReLoadInfo();
		SheetManager.SaveActiveCharacter();
	}
	
	IEnumerator AddExp()
	{
		List<SetterWindowDescription> exp = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("Exp: ", "0", true)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Add Experience", exp));
		
		int e;
		if(int.TryParse(exp[0].outVariable, out e))
		{
			SheetManager.activeCharacter.Experience += e;
			
			if(SheetManager.activeCharacter.Experience - e < SheetManager.activeCharacter.nextLevelExperience &&
				SheetManager.activeCharacter.Experience >= SheetManager.activeCharacter.nextLevelExperience)
			{
				HelperFunctions.ConfirmationWindow("DING! \n\n" + SheetManager.activeCharacter.name + " gained enough experience to level up.", null);
			}
		}
		
		ReLoadInfo();
	}
	
	IEnumerator SetExp()
	{
		List<SetterWindowDescription> exp = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("Exp: ", SheetManager.activeCharacter.Experience.ToString(), true)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Set Experience", exp));
		
		int e;
		if(int.TryParse(exp[0].outVariable, out e))
		{
			SheetManager.activeCharacter.Experience = e;
		}
		
		ReLoadInfo();
	}
	
	void LevelUp()
	{
		BrowserManager.CreateClassBrowser(gameObject, "levelUpClassCallback", new List<string>());
	}
	
	void levelUpClassCallback(List<string> classNames)
	{
		if(!string.IsNullOrEmpty(classNames[0]))
		{
			string className = classNames[0];
			CharacterClass selectedClass = SheetManager.activeCharacter.classes.Find(c => c.name == className);
			if(selectedClass == null)
			{
				selectedClass = ClassManager.Load(className);
				if(selectedClass != null)
				{
					SheetManager.activeCharacter.classes.Add(selectedClass);
					SheetManager.activeCharacter._classSkills.AddRange(selectedClass.classSkills);
				}
			}
			
			string changes = selectedClass.LevelUp();
			
			StartCoroutine(HelperFunctions.ConfirmationWindow(changes, null));
			
			SheetManager.SaveActiveCharacter(false);
			
			ReLoadInfo();
		}
	}
	
	IEnumerator AlignmentClicked()
	{	
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("", "", false, CharacterSheet.ValidAlignments)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Alignment?", updates));
		
		SheetManager.activeCharacter.Alignment = updates[0].outVariable;
		
		alignment.text = SheetManager.activeCharacter.Alignment;
		SheetManager.SaveActiveCharacter();
	}
	
	IEnumerator HealthClicked()
	{
		yield return StartCoroutine(HelperFunctions.HealthWindow(SheetManager.activeCharacter));
		ReLoadInfo();
		SheetManager.SaveActiveCharacter();
	}
	
	IEnumerator SpeedClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("B. Spd.", SheetManager.activeCharacter.BonusSpeed.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Alter Bonus Speed?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetBonusSpeed(updates[0].outVariable);
		if(accepted)
		{
			speed.text = SheetManager.activeCharacter.Speed.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator DRClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("DR", SheetManager.activeCharacter.extraInfo.DR.ToString(), false),
				new SetterWindowDescription("Type", SheetManager.activeCharacter.extraInfo.DRMaterial, true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Damage Reduction?",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetDR(updates[1].outVariable, updates[0].outVariable);
		if(accepted)
		{
			DR.text = SheetManager.activeCharacter.extraInfo.DR + "/" + SheetManager.activeCharacter.extraInfo.DRMaterial;
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator SRClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("S.R.", SheetManager.activeCharacter.extraInfo.SpellResistance.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Spell Resistance?",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetSR(updates[0].outVariable);
		if(accepted)
		{
			SR.text = SheetManager.activeCharacter.extraInfo.SpellResistance.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator strClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Strength", SheetManager.activeCharacter.StrengthModifier));
	}
	
	IEnumerator strPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("STR", SheetManager.activeCharacter.Strength.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Strength Stat?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetStat("Strength", updates[0].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator dexClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Dexterity", SheetManager.activeCharacter.DexterityModifier));
	}
	
	IEnumerator dexPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Dex", SheetManager.activeCharacter.Dexterity.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Dexterity Stat?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetStat("Dexterity", updates[0].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator conClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Constitution", SheetManager.activeCharacter.ConstitutionModifier));
	}
	
	IEnumerator conPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Con", SheetManager.activeCharacter.Constitution.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Constitution Stat?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetStat("Constitution", updates[0].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator IntClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Intelligence", SheetManager.activeCharacter.IntelligenceModifier));
	}
	
	IEnumerator IntPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Int", SheetManager.activeCharacter.Intelligence.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Intelligence Stat?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetStat("Intelligence", updates[0].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator wisClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Wisdom", SheetManager.activeCharacter.WisdomModifier));
	}
	
	IEnumerator wisPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Wis", SheetManager.activeCharacter.Wisdom.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Wisdom Stat?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetStat("Wisdom", updates[0].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator chaClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Charisma", SheetManager.activeCharacter.CharismaModifier));
	}
	
	IEnumerator chaPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Cha", SheetManager.activeCharacter.Charisma.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Charisma Stat?",updates));
		
		bool accepted = SheetManager.activeCharacter.SetStat("Charisma", updates[0].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator altMovementsClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Fly Sp.", SheetManager.activeCharacter.extraInfo.FlySpeed.ToString(), false),
				new SetterWindowDescription("Air Mnv.", SheetManager.activeCharacter.extraInfo.FlyManeuverability, true),
				new SetterWindowDescription("Swim Sp.", SheetManager.activeCharacter.extraInfo.SwimSpeed.ToString(), false),
				new SetterWindowDescription("ClimbSp.", SheetManager.activeCharacter.extraInfo.ClimbSpeed.ToString(), false),
				new SetterWindowDescription("Burr.Sp.", SheetManager.activeCharacter.extraInfo.BurrowSpeed.ToString(), false),
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Modify Alt. Movement?",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetSpeeds(	updates[0].outVariable,updates[1].outVariable,
																			updates[2].outVariable,updates[3].outVariable,
																			updates[4].outVariable);
		if(accepted)
		{
			ReLoadInfo();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator hairClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("HairCol.", SheetManager.activeCharacter.extraInfo.HairColor, true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Set Hair Color",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetHairColor(updates[0].outVariable);
		if(accepted)
		{
			hair.text = SheetManager.activeCharacter.extraInfo.HairColor;
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator eyesClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("EyeCol.", SheetManager.activeCharacter.extraInfo.EyeColor, true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Set Eye Color",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetEyeColor(updates[0].outVariable);
		if(accepted)
		{
			eyes.text = SheetManager.activeCharacter.extraInfo.EyeColor;
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator skinClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Tone", SheetManager.activeCharacter.extraInfo.SkinColor, true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Set Skin Tone",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetSkinColor(updates[0].outVariable);
		if(accepted)
		{
			skin.text = SheetManager.activeCharacter.extraInfo.SkinColor;
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator heightClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Ft:", Mathf.Floor(SheetManager.activeCharacter.extraInfo.Height/12).ToString(), false),
				new SetterWindowDescription("In:", (SheetManager.activeCharacter.extraInfo.Height%12).ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Set Height",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetHeight(updates[0].outVariable,updates[1].outVariable);
		if(accepted)
		{
			height.text = Mathf.FloorToInt(SheetManager.activeCharacter.extraInfo.Height/12) + "'" + SheetManager.activeCharacter.extraInfo.Height%12 + "\""; 
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator ageClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Years ", Mathf.Floor(SheetManager.activeCharacter.extraInfo.Age).ToString(), true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Set Character Age",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetAge(updates[0].outVariable);
		if(accepted)
		{
			age.text = SheetManager.activeCharacter.extraInfo.Age.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator fortClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Fortitude", SheetManager.activeCharacter.Fort));
	}
	
	IEnumerator fortPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Misc", SheetManager.activeCharacter.BonusFort.ToString(), false)
			};
		
		int rawFort = SheetManager.activeCharacter.Fort - SheetManager.activeCharacter.Constitution.Modifier() - SheetManager.activeCharacter.BonusFort;
		int conFort = SheetManager.activeCharacter.Constitution.Modifier();
		int bonusFort = SheetManager.activeCharacter.BonusFort;
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( rawFort + "(Class) + " + conFort + "(Con) + " + bonusFort + "(Misc) = " + (rawFort+conFort+bonusFort),updates));
		
		bool accepted = SheetManager.activeCharacter.SetBonusFort(updates[0].outVariable);
		if(accepted)
		{
			fort.text = SheetManager.activeCharacter.Fort.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator reflClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Reflex", SheetManager.activeCharacter.Ref));
	}
	
	IEnumerator reflPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Misc", SheetManager.activeCharacter.BonusRef.ToString(), false)
			};
		
		int raw = SheetManager.activeCharacter.Ref - SheetManager.activeCharacter.Dexterity.Modifier() - SheetManager.activeCharacter.BonusRef;
		int stat = SheetManager.activeCharacter.Dexterity.Modifier();
		int misc = SheetManager.activeCharacter.BonusRef;
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( raw + "(Class) + " + stat + "(Dex) + " + misc + "(Misc) = " + (raw+stat+misc),updates));
		
		bool accepted = SheetManager.activeCharacter.SetBonusRef(updates[0].outVariable);
		if(accepted)
		{
			refl.text = SheetManager.activeCharacter.Ref.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator willClicked()
	{
		yield return StartCoroutine(HelperFunctions.RollWindow("Will", SheetManager.activeCharacter.Will));
	}
	
	IEnumerator willPressed()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("Misc", SheetManager.activeCharacter.BonusWill.ToString(), false)
			};
		
		int raw = SheetManager.activeCharacter.Will - SheetManager.activeCharacter.Wisdom.Modifier() - SheetManager.activeCharacter.BonusWill;
		int stat = SheetManager.activeCharacter.Wisdom.Modifier();
		int misc = SheetManager.activeCharacter.BonusWill;
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( raw + "(Class) + " + stat + "(Wis) + " + misc + "(Misc) = " + (raw+stat+misc),updates));
		
		bool accepted = SheetManager.activeCharacter.SetBonusWill(updates[0].outVariable);
		if(accepted)
		{
			will.text = SheetManager.activeCharacter.Will.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
}
